<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;

use app\admin\model\Qinu;
use think\Db;
use think\Request;
use think\Validate;

class Conf
{
    public function lst()
    {
        //数据分页，每页显示5条数据
        $data = Db::name('conf')->select();
//        echo '<pre>';print_r($data);exit;
        //渲染配置列表
        return view('conf_lst',['data'=>$data]);
    }

    public function conf()
    {
        $data = Db::name('conf')->select();
        $str = '';
        foreach ($data as $value){
            if($value['type']=='单行文本'){
                $str .= '<div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>'.$value['cnname'].'</label>
<div class="formControls col-xs-8 col-sm-9">
 <input type="text" id="website-title" placeholder="控制在25个字、50个字节以内" value="'.$value['value'].'" name="'.$value['enname'].'" class="input-text">
</div>
</div>';
            }elseif ($value['type']=='多行文本'){
                $str .= '<div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>'.$value['cnname'].'</label>
<div class="formControls col-xs-8 col-sm-9">
 <textarea type="text" id="website-title" name="'.$value['enname'].'" class="input-text" style="height:50px">'.$value['value'].'</textarea>
</div>
</div>';
            }elseif ($value['type']=='单选框'){
                $value['values'] = json_decode($value['values'],true);
                $str .= '<div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>'.$value['cnname'].'</label>
<div class="formControls col-xs-8 col-sm-9">';
                foreach ($value['values'] as $values){
                    if($value['value']==$values){
                        $str .= '<input type="radio" name="'.$value['enname'].'" value="'.$values.'" checked="checked"/>'.$values;
                    }else{
                        $str .= '<input type="radio" name="'.$value['enname'].'" value="'.$values.'"/>'.$values;
                    }
                }
$str.='</div>
</div>';
            }elseif ($value['type']=='下拉框'){
                $value['values'] = json_decode($value['values'],true);
                $str .= '<div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>'.$value['cnname'].'</label>
<div class="formControls col-xs-8 col-sm-9"><select name="'.$value['enname'].'">';
                foreach ($value['values'] as $values){
                    if($value['value']==$values){
                        $str .= '<option value="'.$values.'" selected>'.$values.'</option>';
                    }else{
                        $str .= '<option value="'.$values.'">'.$values.'</option>';
                    }
                }
                $str.='</select></div>
</div>';
            }elseif ($value['type']=='图片'){
                $str .= '<div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>'.$value['cnname'].'</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="file" name="'.$value['enname'].'" onchange="yulan(this)"></div></div><div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>预览</label>
<div class="formControls col-xs-8 col-sm-9 shows">';
                if($value['value']){
                    $str .= '<img src="'.$value['value'].'" width="50"/>';
                }

$str.='</div></div>';
            }
        }
//        return $str;
        return view('system_base',['str'=>$str]);
    }

    /**加载添加配置项
     * @return \think\response\View
     */
    public function add(){
        return view('add_conf');
    }

    /**添加配置
     * @param Request $request
     * @return \think\response\Json
     */
    public function add_conf(Request $request){
        $data = $request->post();
        $val = new Validate([
            'cnname'=>'require',
            'enname'=>'require',
            'type'=>'require',
        ]);
        if(!$val->check($data)){
            return json(['code'=>201,'msg'=>$val->getError()]);
        }
        if(!$data['values']){
            unset($data['values']);
        }else{
            $data['values'] = explode(';',$data['values']);
            $data['values'] = json_encode($data['values']);
        }
        if(Db::name('conf')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**删除配置
     * @param Request $request
     * @return \think\response\Json
     */
    public function del_conf(Request $request){
        $id = $request->param('id',null);
        $thisd = Db::name('conf')->where('id',$id)->find();
        if($thisd['type']=='图片'){
            if($thisd['value']){
                Qinu::DeleFile($thisd['value']);
            }
        }
        if(Db::name('conf')->where('id',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**修改配置
     * @param Request $request
     * @return \think\response\View
     */
    public function edit_cof(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('conf')->where('id',$id)->find();
        $str ='';
        if($data['values']){
            $data['values'] = json_decode($data['values'],true);
            foreach ($data['values'] as $value){
                $str .=$value.';';
            }
        }
        $data['values'] = $str;
        return view('edit_conf',['data'=>$data]);
    }

    /**修改配置
     * @param Request $request
     * @return \think\response\Json
     */
    public function edits_conf(Request $request){
        $data = $request->post();
        $val = new Validate([
            'cnname'=>'require',
            'enname'=>'require',
            'type'=>'require',
        ]);
        if(!$val->check($data)){
            return json(['code'=>201,'msg'=>$val->getError()]);
        }
        if(!$data['values']){
            unset($data['values']);
        }else{
            $data['values'] = explode(';',$data['values']);
            $data['values'] = json_encode($data['values']);
        }
        if(Db::name('conf')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败']);
        }

    }

    /**修改数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function addthis(Request $request){
        $data = $request->post();
//        return $data;
        $file = $request->file();
        $lao = Db::name('conf')->select();
        foreach ($lao as $value){
            if($value['type']=='图片'){
                if(isset($file[$value['enname']])){
                    $tupian = Qinu::Upfile($file[$value['enname']]);
                    if($tupian){
                        Db::name('conf')->where('id',$value['id'])->update(['value'=>$tupian]);
                    }
                }
            }else{
                if(isset($data[$value['enname']])){
                    Db::name('conf')->where('id',$value['id'])->update(['value'=>$data[$value['enname']]]);
                }

            }
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }
}